﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using UDS.Entity;
using UDS.Components;

namespace UDS.WebApp.SubModule
{
    public partial class AddFilePage : PageBase
    {
        /// <summary>
        /// The user must have the "ManageCommonFile" permission.
        /// </summary>
        public override PermissionType RequiredPermission
        {
            get { return PermissionType.ManageCommonFile; }
        }

        protected override void OnInit(EventArgs e)
        {
            // Hoold event handlers
            btnNew.Click += new EventHandler(OnCreateFileClick);
            base.OnInit(e);
        }

        private void OnCreateFileClick(object sender, EventArgs e)
        {
            Page.Validate();
            if (Page.IsValid)
            {

                CFFileInfo fileInfo = new CFFileInfo();
                fileInfo.Name = txtName.Text;
                fileInfo.Version = txtVersion.Text;
                fileInfo.CatalogID = listCatalog.SelectedCatalog;
                fileInfo.Invalid = cbInvalid.Checked;
                fileInfo.Creator = SiteContext.Current.UserName;

                try
                {
                    CommonFile.AddFile(fileInfo, fileUpload.PostedFile);
                    SiteContext.Current.AlertAndRedirect("成功添加新文件。", "FileList.aspx");
                }
                catch (Exception ex)
                {
                    SiteContext.Current.AlertAndRedirect("新增文件失败：" + ex.Message, "FileList.aspx");
                }
            }
        }
    }
}
